// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   IBatisBuilder.java

package tool.radmodel;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

// Referenced classes of package com.qdone.radmodel:
//			CodeBuilder, Project, Entity, Field

public class IBatisBuilder implements CodeBuilder {

	private Project project;

	public IBatisBuilder(Project project) {
		this.project = project;
	}

	// 将数据库字段类型转成java类型
	private String getDataType(String dbType) {
		// 根据字段类型的值，返回java的数据类型
		String dataType = "";
		if ("INT".equals(dbType)) {
			dataType = "INTEGER";
		} else {
			dataType = dbType;
		}
		return dataType;
	}

	public void saveToFile(String iBatisConfigContent) {
		String fileName = (new StringBuilder(String.valueOf(project.getEntitiesPath()))).append(project.getEntity().getEntityName()).append("Mapper.xml").toString();
		File file = new File(fileName);
		if (file.exists())
			file.delete();
		try {
			FileUtils.writeStringToFile(file, iBatisConfigContent, "utf-8");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void saveToFile() {
		StringBuffer springConfigBuffer = new StringBuffer();
		Entity entity = project.getEntity();
		springConfigBuffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
		springConfigBuffer.append("<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">\r\n\r\n");
		springConfigBuffer.append((new StringBuilder("<mapper namespace=\"")).append(project.getDaoImplPackage()).append(".").append(entity.getEntityName()).append("Dao\">\r\n\r\n").toString());
		// springConfigBuffer.append((new
		// StringBuilder("<typeAlias alias=\"")).append(entity.getEntityName()).append("\" type=\"").append(project.getEntitiesPackage()).append(".").append(entity.getEntityName()).append("\"/>\r\n\r\n").toString());
		springConfigBuffer.append((new StringBuilder("    <resultMap id=\"")).append(entity.getEntityName()).append("Map\" type=\"").append(project.getEntitiesPackage()).append(".")
				.append(entity.getEntityName()).append("\">\r\n").toString());
		springConfigBuffer.append((new StringBuilder("        <result property=\"")).append(entity.getPrimaryKeyField().getPropertyName()).append("\" column=\"")
				.append(entity.getPrimaryKeyField().getFieldName()).append("\"/>\r\n").toString());
		for (int i = 0; i < entity.getFields().size(); i++) {
			Field field = (Field) entity.getFields().get(i);
			springConfigBuffer.append((new StringBuilder("        <result property=\"")).append(field.getPropertyName()).append("\" column=\"").append(field.getFieldName()).append("\"/>\r\n")
					.toString());
		}

		springConfigBuffer.append("    </resultMap>\r\n\r\n");
		springConfigBuffer.append("    <sql id=\"allColumns\">\r\n");
		springConfigBuffer.append((new StringBuilder("        ")).append(entity.getPrimaryKeyField().getFieldName()).toString());
		int count = 1;
		for (int i = 0; i < entity.getFields().size(); i++) {
			if (count % 6 == 0)
				springConfigBuffer.append((new StringBuilder(",\r\n        ")).append(((Field) entity.getFields().get(i)).getFieldName()).toString());
			else
				springConfigBuffer.append((new StringBuilder(",")).append(((Field) entity.getFields().get(i)).getFieldName()).toString());
			count++;
		}

		springConfigBuffer.append("\r\n    </sql>\r\n\r\n");

		springConfigBuffer.append("    <sql id=\"Columns\">\r\n");
		int count1 = 1;
		for (int i = 0; i < entity.getFields().size(); i++) {
			if (i == 0) {
				if (count1 % 6 == 0)
					springConfigBuffer.append((new StringBuilder("        ")).append(new StringBuilder("\r\n        ")).append(((Field) entity.getFields().get(i)).getFieldName()).toString());
				else
					springConfigBuffer.append((new StringBuilder("        ")).append(new StringBuilder("")).append(((Field) entity.getFields().get(i)).getFieldName()).toString());
			} else {
				if (count1 % 6 == 0)
					springConfigBuffer.append((new StringBuilder(",\r\n        ")).append(((Field) entity.getFields().get(i)).getFieldName()).toString());
				else
					springConfigBuffer.append((new StringBuilder(",")).append(((Field) entity.getFields().get(i)).getFieldName()).toString());
			}

			count1++;
		}

		springConfigBuffer.append("\r\n    </sql>\r\n\r\n");

		springConfigBuffer.append("    <sql id=\"dynamicWhere\">\r\n");
		springConfigBuffer.append("   	WHERE 1=1\r\n");
		/* 生成主键dynamicWhere */
		String primaryKey = entity.getPrimaryKeyField().getPropertyName();
		springConfigBuffer.append("      <if test=\"null!=").append(primaryKey).append(" and ''!=").append(primaryKey).append("\">\r\n");
		springConfigBuffer.append("         AND ").append(entity.getPrimaryKeyField().getFieldName()).append(" = #{").append(primaryKey).append(",jdbcType=")
				.append(getDataType(entity.getPrimaryKeyField().getDateTypeName())).append("}\r\n");
		springConfigBuffer.append("      </if>\r\n");
		for (int i = 0; i < entity.getFields().size(); i++) {
			String propertyName = ((Field) entity.getFields().get(i)).getPropertyName();
			springConfigBuffer.append("      <if test=\"null!=").append(propertyName);
			if (((Field) entity.getFields().get(i)).getDateTypeName().equals("VARCHAR")) {
				springConfigBuffer.append(" and ''!=").append(propertyName);
			}
			springConfigBuffer.append("\">\r\n");
			springConfigBuffer.append("        AND ").append(((Field) entity.getFields().get(i)).getFieldName()).append(" = #{").append(((Field) entity.getFields().get(i)).getPropertyName())
					.append(",jdbcType=").append(getDataType(((Field) entity.getFields().get(i)).getDateTypeName())).append("}\r\n");
			springConfigBuffer.append("      </if>\r\n");
		}

		springConfigBuffer.append("    </sql>\r\n\r\n");
		// selectList
		springConfigBuffer.append((new StringBuilder("    <!-- ")).append(entity.getSelectListSqlName()).append(" -->\r\n").toString());
		springConfigBuffer.append((new StringBuilder("    <select id=\"")).append(entity.getSelectListSqlName()).append("\" parameterType=\"java.util.HashMap\" resultMap=\"")
				.append(entity.getEntityName()).append("Map\">\r\n").toString());
		springConfigBuffer.append("      SELECT <include refid=\"allColumns\" /> \r\n");
		springConfigBuffer.append((new StringBuilder("      FROM ")).append(entity.getTableName()).append(" \r\n").toString());
		// springConfigBuffer.append("      <dynamic prepend=\"WHERE\">\r\n");
		springConfigBuffer.append("         <include refid=\"dynamicWhere\"/>\r\n");
		springConfigBuffer.append("     <if test=\"orderByClause != null and ''!= orderByClause\" >\r\n");
		springConfigBuffer.append("         order by ${orderByClause}\r\n");
		springConfigBuffer.append("     </if>\r\n");
		springConfigBuffer.append("     <if test=\"null != offset\">\r\n");
		springConfigBuffer.append("         LIMIT	#{offset}, #{limit}\r\n");
		springConfigBuffer.append("     </if>\r\n");
		
		// springConfigBuffer.append("      </dynamic>\r\n");
		// springConfigBuffer.append("      <dynamic prepend=\"ORDER BY\">\r\n");
		// springConfigBuffer.append("         <isNotEmpty property=\"fieldSort\"> $fieldSort$ \r\n");
		// springConfigBuffer.append("              <isNotEmpty property=\"sortOrder\"> $sortOrder$ </isNotEmpty>\r\n");
		// springConfigBuffer.append("        </isNotEmpty>\r\n");
		// springConfigBuffer.append("      </dynamic>\r\n");
		springConfigBuffer.append("    </select>\r\n\r\n");
		// selectCount
		springConfigBuffer.append((new StringBuilder("    <!-- ")).append(entity.getSelectListSqlName()).append(" -->\r\n").toString());
		springConfigBuffer.append((new StringBuilder("    <select id=\"")).append(entity.getSelectCountSqlName())
				.append("\" parameterType=\"java.util.HashMap\" resultType=\"java.lang.Integer\">\r\n").toString());
		springConfigBuffer.append("      SELECT COUNT(*) \r\n");
		springConfigBuffer.append((new StringBuilder("      FROM ")).append(entity.getTableName()).append(" \r\n").toString());
		// springConfigBuffer.append("      <dynamic prepend=\"WHERE\">\r\n");
		springConfigBuffer.append("         <include refid=\"dynamicWhere\"/>\r\n");
		// springConfigBuffer.append("      </dynamic>\r\n");
		// springConfigBuffer.append("      <dynamic prepend=\"ORDER BY\">\r\n");
		// springConfigBuffer.append("         <isNotEmpty property=\"fieldSort\"> $fieldSort$ \r\n");
		// springConfigBuffer.append("              <isNotEmpty property=\"sortOrder\"> $sortOrder$ </isNotEmpty>\r\n");
		// springConfigBuffer.append("        </isNotEmpty>\r\n");
		// springConfigBuffer.append("      </dynamic>\r\n");
		springConfigBuffer.append("    </select>\r\n\r\n");
		// selectByPk
		springConfigBuffer.append((new StringBuilder("    <!-- ")).append(entity.getSelectSqlName()).append(" -->\r\n").toString());
		springConfigBuffer.append((new StringBuilder("    <select id=\"")).append(entity.getSelectByPkName()).append("\" parameterType=\"java.lang.String\"  resultMap=\"")
				.append(entity.getEntityName()).append("Map\">\r\n").toString());
		springConfigBuffer
				.append("      SELECT <include refid=\"allColumns\" />\r\n")
				.append((new StringBuilder("      FROM ")).append(entity.getTableName()).append(" WHERE ").toString())
				.append(entity.getPrimaryKeyField().getFieldName())
				.append((new StringBuilder(" = #{")).append(entity.getPrimaryKeyField().getPropertyName()).append(",jdbcType=").append(getDataType(entity.getPrimaryKeyField().getDateTypeName()))
						.append("}").toString()).append("\r\n");
		springConfigBuffer.append("    </select>\r\n\r\n");
		// insert
		springConfigBuffer.append((new StringBuilder("    <!-- ")).append(entity.getInsertSqlName()).append(" -->\r\n").toString());
		springConfigBuffer.append((new StringBuilder("    <insert id=\"")).append(entity.getInsertSqlName()).append("\" parameterType=\"").append(project.getEntitiesPackage()).append(".")
				.append(entity.getEntityName()).append("\">\r\n").toString());
		springConfigBuffer.append((new StringBuilder("      INSERT INTO ")).append(entity.getTableName()).append(" (<include refid=\"Columns\" />) VALUES(\r\n").toString());
		count = 1;
		for (int i = 0; i < entity.getFields().size(); i++) {
			if (i == 0) {
				if (count % 3 == 0)
					springConfigBuffer.append((new StringBuilder("        #{")).append(((Field) entity.getFields().get(i)).getPropertyName()).append(",jdbcType=")
							.append(getDataType(((Field) entity.getFields().get(i)).getDateTypeName())).append("}").toString());
				else
					springConfigBuffer.append((new StringBuilder("        #{")).append(((Field) entity.getFields().get(i)).getPropertyName()).append(",jdbcType=")
							.append(getDataType(((Field) entity.getFields().get(i)).getDateTypeName())).append("}").toString());
			} else {
				if (count % 3 == 0)
					springConfigBuffer.append((new StringBuilder(",\r\n        #{")).append(((Field) entity.getFields().get(i)).getPropertyName()).append(",jdbcType=")
							.append(getDataType(((Field) entity.getFields().get(i)).getDateTypeName())).append("}").toString());
				else
					springConfigBuffer.append((new StringBuilder(",\r\n        #{")).append(((Field) entity.getFields().get(i)).getPropertyName()).append(",jdbcType=")
							.append(getDataType(((Field) entity.getFields().get(i)).getDateTypeName())).append("}").toString());
			}
			count++;
		}

		springConfigBuffer.append("\r\n        )\r\n");
		springConfigBuffer.append("    </insert>\r\n\r\n");
		// insertList
		springConfigBuffer.append((new StringBuilder("    <!-- ")).append(entity.getInsertListSqlName()).append(" -->\r\n").toString());
		springConfigBuffer.append((new StringBuilder("    <insert id=\"")).append(entity.getInsertListSqlName()).append("\" parameterType=\"java.util.List\">\r\n").toString());
		springConfigBuffer.append((new StringBuilder("      INSERT INTO ")).append(entity.getTableName()).append(" (<include refid=\"Columns\" />) \r\n").toString());
		springConfigBuffer.append((new StringBuilder("      VALUES\r\n ")).toString());
		springConfigBuffer.append((new StringBuilder("      <foreach collection=\"list\" item=\"item\" index=\"index\" separator=\",\">\r\n")).toString());
		springConfigBuffer.append("		(");
		count = 1;
		for (int i = 0; i < entity.getFields().size(); i++) {
			if (i == 0) {
				if (count % 3 == 0)
					springConfigBuffer.append((new StringBuilder("\r\n        #{item.")).append(((Field) entity.getFields().get(i)).getPropertyName()).append(",jdbcType=")
							.append(getDataType(((Field) entity.getFields().get(i)).getDateTypeName())).append("}").toString());
				else
					springConfigBuffer.append((new StringBuilder("#{item.")).append(((Field) entity.getFields().get(i)).getPropertyName()).append(",jdbcType=")
							.append(getDataType(((Field) entity.getFields().get(i)).getDateTypeName())).append("}").toString());
			} else {
				if (count % 3 == 0)
					springConfigBuffer.append((new StringBuilder(",\r\n        #{item.")).append(((Field) entity.getFields().get(i)).getPropertyName()).append(",jdbcType=")
							.append(getDataType(((Field) entity.getFields().get(i)).getDateTypeName())).append("}").toString());
				else
					springConfigBuffer.append((new StringBuilder(",#{item.")).append(((Field) entity.getFields().get(i)).getPropertyName()).append(",jdbcType=")
							.append(getDataType(((Field) entity.getFields().get(i)).getDateTypeName())).append("}").toString());
			}
			count++;
		}

		springConfigBuffer.append("\r\n        )\r\n");
		springConfigBuffer.append(" 		</foreach>");
		springConfigBuffer.append("\r\n    </insert>\r\n\r\n");
		// update
		springConfigBuffer.append((new StringBuilder("    <!-- ")).append(entity.getUpdateSqlName()).append(" -->\r\n").toString());
		springConfigBuffer.append((new StringBuilder("    <update id=\"")).append(entity.getUpdateSqlName()).append("\" parameterType=\"").append(project.getEntitiesPackage()).append(".")
				.append(entity.getEntityName()).append("\">\r\n").toString());
		springConfigBuffer.append((new StringBuilder("      UPDATE ")).append(entity.getTableName()).append(" \r\n	<trim prefix=\"SET\" suffixOverrides=\",\">\r\n").toString());
		String strSQL = "";
		for (int i = 0; i < entity.getFields().size(); i++) {
			String propertyName = ((Field) entity.getFields().get(i)).getPropertyName();
			strSQL = (new StringBuilder(String.valueOf(strSQL))).append("\t<if test=\"null!=").append(propertyName).toString();
			if (((Field) entity.getFields().get(i)).getDateTypeName().equals("VARCHAR")) {
				strSQL = (new StringBuilder(String.valueOf(strSQL))).append(" and ''!=").append(propertyName).toString();
			}
			strSQL = (new StringBuilder(String.valueOf(strSQL))).append(" \">").append(((Field) entity.getFields().get(i)).getFieldName()).append(" = #{")
					.append(((Field) entity.getFields().get(i)).getPropertyName()).append(",jdbcType=").append(getDataType(((Field) entity.getFields().get(i)).getDateTypeName())).toString();
			if (i == entity.getFields().size() - 1) {
				strSQL = (new StringBuilder(String.valueOf(strSQL))).append("} </if>\r\n      ").toString();
			} else {
				strSQL = (new StringBuilder(String.valueOf(strSQL))).append("}, </if>\r\n      ").toString();
			}

		}

		strSQL = (new StringBuilder(String.valueOf(strSQL))).append("</trim>\r\n").toString();
		springConfigBuffer.append(strSQL);

		springConfigBuffer
				.append("		WHERE ")
				.append(entity.getPrimaryKeyField().getFieldName())
				.append((new StringBuilder(" = #{")).append(entity.getPrimaryKeyField().getPropertyName()).append(",jdbcType=").append(getDataType(entity.getPrimaryKeyField().getDateTypeName()))
						.append("}").toString()).append("\r\n");
		springConfigBuffer.append("    </update>\r\n\r\n").toString();
		// deleteByMap
		springConfigBuffer.append((new StringBuilder("    <!-- ")).append(entity.getDeleteSqlName()).append(" -->\r\n").toString());
		springConfigBuffer.append((new StringBuilder("    <delete id=\"")).append(entity.getDeleteSqlName()).append("\" parameterType=\"java.util.HashMap\">\r\n").toString());
		springConfigBuffer.append((new StringBuilder("      DELETE FROM ")).append(entity.getTableName())).append("\r\n");
		// springConfigBuffer.append("      <dynamic prepend=\"WHERE\">\r\n");
		springConfigBuffer.append("         <include refid=\"dynamicWhere\"/>\r\n");
		// springConfigBuffer.append("      </dynamic>\r\n");
		springConfigBuffer.append("    </delete>\r\n\r\n");
		// deleteByPk
		springConfigBuffer.append((new StringBuilder("    <!-- ")).append(entity.getDeleteSqlName()).append(" -->\r\n").toString());
		springConfigBuffer.append((new StringBuilder("    <delete id=\"")).append(entity.getDeleteByPkName()).append("\" parameterType=\"java.util.HashMap\">\r\n").toString());
		springConfigBuffer.append((new StringBuilder("      DELETE FROM ")).append(entity.getTableName())).append("\r\n");
		springConfigBuffer
				.append("		WHERE ")
				.append(entity.getPrimaryKeyField().getFieldName())
				.append((new StringBuilder(" = #{")).append(entity.getPrimaryKeyField().getPropertyName()).append(",jdbcType=").append(getDataType(entity.getPrimaryKeyField().getDateTypeName()))
						.append("}").toString()).append("\r\n");
		springConfigBuffer.append("    </delete>\r\n\r\n");
		springConfigBuffer.append("</mapper>");
		saveToFile(springConfigBuffer.toString());
	}
}
